// API
import { GetPermission } from "@/api/user";
// utils
import { formatTree } from "@/utils/format";
import { shallowRef } from "vue";

/** 格式化菜单数据生成路由 */
const formatRouterMenu = (data) => {

    // 检测数据是否存在
    if (data && (!Array.isArray(data) || data.length === 0)) {
        return false
    }
    // console.log("数据存在",data)
    // 监听存储数据
    const router = [];
    // 遍历菜单
    data.forEach(item => {
        // console.log("菜单遍历item：",item)
        const router_obj = {};
        // id
        router_obj.menu_id = item.resourceId;
        router_obj.parent_id = item.parentId;
        // path
        router_obj.path = item.routerPath;
        // name
        router_obj.name = item.routerName;
        // resourceType
        router_obj.resource_type = item.resourceType;
        // hidden
        if (item.hidden === "1") {
            router_obj.hidden = true;
        } else {
            router_obj.hidden = false
        }
        // redirect
        if (item.redirect) {
            router_obj.redirect = item.redirect;
        }
        // meta
        router_obj.meta = {
            // 菜单名称
            title: item.resourceName,
            // 菜单图标
            icon: item.icon,
            // 是否缓存页面
            keepAlive: item.keepAlive === "1" ? true : false
        }
        // component
        router_obj.component = shallowRef(require(`@/${item.component}`).default);
        // 添加至router数组
        router.push(router_obj);
    })
    // console.log("router是：",router)
    // 树状格式化
    const tree_data = formatTree(router, "menu_id", "parent_id", "children", 0);
    // console.log("tree树：",tree_data)
    // 返回数据
    return tree_data;

}
const state = {
    dynamic_router: [],
    permission_element: "",
    user_type: "",
    user_role: "",
    user_id: "",
};
const getters = {
    dynamic_router: state => state.dynamic_router,
    permission_element: state => state.permission_element,
    user_type: state => state.user_type,
    user_role: state => state.user_role,
    user_id: state => state.user_id,
}
const mutations = {
    SET_DYNAMIC_ROUTER(state, value) {
        state.dynamic_router = value;
    },
    SET_PERMISSION_ELEMENT(state, value) {
        state.permission_element = value;
    },
    SET_USERTYPE(state, value) {
        state.user_type = value;
    },
    SET_USERROLE(state, value) {
        state.user_role = value;
    },
    SET_USERID(state, value) {
        state.user_id = value
    }
}
const actions = {
    getRouterAction(context) {
        // console.log("获取用户权限数据Action context",context)
        return new Promise((resolve, reject) => {
            GetPermission().then((response) => {
                console.log(response);
                // console.log("获取用户权限数据Action response",response)
                const data = response.data.routerMenuList;
                window.sessionStorage.setItem("firstPath" , response.data.path)
                window.sessionStorage.setItem("firstName" , response.data.name)
                // console.log("获取用户权限数据Action data",data)
                // 格式化菜单数据生成路由
                const menu = formatRouterMenu(data);
                console.log("获取用户权限数据Action menu",menu)
                // window.sessionStorage.setItem("defaultPath" , response.data.path)
                // 存储路由
                context.commit("SET_DYNAMIC_ROUTER", menu);
                // 存储元素权限 
                context.commit("SET_PERMISSION_ELEMENT", response.data.permissionElementList);
                // 更新用户类型
                context.commit("SET_USERTYPE", response.data.superAdmin);
                // 存储用户角色
                context.commit("SET_USERROLE", response.data.roleInfoList);
                // 用户id
                context.commit("SET_USERID", response.data.userId);
                resolve(response);
            }).catch(error => {
                reject(error);
            })
        })
    }
}
export default {
    namespaced: true,
    state,
    getters,
    mutations,
    actions
};
